package cn.itsource.ldj.base.controller;

import cn.itsource.ldj.util.AjaxResult;
import cn.itsource.ldj.util.FastdfsUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("/fastDfs")
public class FastDfsController {

    @RequestMapping("/upload")
    public AjaxResult upload(MultipartFile file) throws IOException {
//        System.out.println(file);
        // 获取源文件名
        String filename = file.getOriginalFilename();
        // 获取文件名后缀
        String fileSuffix = filename.substring(filename.lastIndexOf(".") + 1);

        // 上传文件
        String path = FastdfsUtil.upload(file.getBytes(),fileSuffix);
//        System.out.println(path);
        // 返回文件路径给前端
        return AjaxResult.me().setResultObj(path);
    }

}
